﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.MailClient.Win.Controls.Helper;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGauges.Win.Gauges.State;
using Zero.POCO;
using Zero.POCO.Donnees;
using Zero.POCO.Views;

namespace DevExpress.MailClient.Win.Controls {
    public partial class WidgetReglagePenetration : UserControl {
        Timer updateTimer = new Timer();
        static Color positiveColor = Color.FromArgb(70, 158, 165);
        static Color negativeColor = Color.FromArgb(165, 70, 113);


        /// <summary>
        /// Helper pour aider la multi selection 
        /// </summary>
        GridCheckMarksSelection gridCheckMarksSA;


        private GridCheckMarksSelection gridCheckMarksVagueSA;
        private List<Vague> listeVaguesViewsRef;

        public event EventHandler<EventArgsProfil> ChangeProfilEvent;
        private List<Vague> _vagues;
        private List<SupportsView> _supports;
        private List<SupportsView> listeSupportsViewsRef;
        public WidgetReglagePenetration(List<Vague> vagues, List<SupportsView> supports,List<Filter> filters )
        {
            InitializeComponent();
            _vagues = vagues;
            _supports = supports;
            lueFiltre.Properties.DataSource = filters;
            lueFiltre.Properties.DisplayMember = "Nom";
            lueSupport.Properties.DataSource = (_supports.Where(e => e.Media == Zero.POCO.Compagne.TypeMedia.TV).Select(e => e.Nom).ToList());
            lueVague.Properties.DataSource = _vagues;
            lueVague.Properties.DisplayMember = "Libelle";

            glueVagueRef.Properties.DataSource = _vagues;
            glueVagueRef.Properties.DisplayMember = "Libelle";

            glueVagueRef.Properties.View.OptionsSelection.MultiSelect = true;
            glueVagueRef.CustomDisplayText += gluVagueRef_CustomDisplayText;
            glueVagueRef.Properties.PopulateViewColumns();

            gridCheckMarksVagueSA = new GridCheckMarksSelection(glueVagueRef.Properties);

            gridCheckMarksVagueSA.SelectionChanged += gridCheckMarksVague_SelectionChanged;
            glueVagueRef.Properties.Tag = gridCheckMarksVagueSA;




            glueSupportsRef.Properties.DataSource = _supports;

            glueSupportsRef.Properties.DisplayMember = "Nom";
            glueSupportsRef.Properties.View.OptionsSelection.MultiSelect = true;
            glueSupportsRef.CustomDisplayText += glueSupportsRef_CustomDisplayText;
            glueSupportsRef.Properties.PopulateViewColumns();


            gridCheckMarksSA = new GridCheckMarksSelection(glueSupportsRef.Properties);

            gridCheckMarksSA.SelectionChanged += new GridCheckMarksSelection.SelectionChangedEventHandler(gridCheckMarks_SelectionChanged);
            glueSupportsRef.Properties.Tag = gridCheckMarksSA;


            cbDistribution.SelectedIndex = 0;



            //gridCheckMarksSA.SelectAll(_supports);



            /*updateTimer.Interval = 1000;
            updateTimer.Tick += OnTimerTick;
            updateTimer.Start();
            OnTimerTick(this, EventArgs.Empty);*/
        }
      
        int index = 0;
       // StockData previousData;
        void OnTimerTick(object sender, EventArgs e) {
          /*  if(IsDisposed) { updateTimer.Stop(); return; }
            StockData data = StockDataCreator.GetData(index);
            if(index > 0) {
                decimal d = data.ClosePrice - previousData.ClosePrice;
                decimal dq = (d * 100) / data.ClosePrice;
                labelControl5.Text = Math.Round( d, 2, MidpointRounding.ToEven).ToString().TrimEnd('0');
                stateIndicatorComponent1.StateIndex =  previousData.ClosePrice - data.ClosePrice < 0 ? 0 : 1;
                if(stateIndicatorComponent1.StateIndex == 0) {
                    labelControl5.ForeColor = positiveColor;
                    labelControl2.Text = data.ClosePrice.ToString().TrimEnd('0') + "<br><color=70,158,165>" + Math.Round(dq, 2, MidpointRounding.ToEven).ToString().TrimEnd('0') + '%';
                }
                else {
                    labelControl5.ForeColor = negativeColor;
                    labelControl2.Text = data.ClosePrice.ToString().TrimEnd('0') + "<br><color=165,70,113>" + Math.Round(dq, 2, MidpointRounding.ToEven).ToString().TrimEnd('0') + '%';
                }
            }
            previousData = data;
            index++;*/
        }
        private void gluVagueRef_CustomDisplayText(object sender, XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            GridCheckMarksSelection gridCheckMark = (sender as GridLookUpEdit).Properties.Tag as GridCheckMarksSelection;
            if (gridCheckMark == null) return;
            listeVaguesViewsRef = new List<Vague>();
            foreach (Vague rv in gridCheckMark.Selection)
            {
                if (sb.ToString().Length > 0) { sb.Append(", "); }
                sb.Append(rv.Libelle);
                listeVaguesViewsRef.Add(rv);
            }
            e.DisplayText = sb.ToString();
        }
        void gridCheckMarksVague_SelectionChanged(object sender, EventArgs e)
        {
            if (ActiveControl is GridLookUpEdit)
            {
                int i = 0;
                StringBuilder sb = new StringBuilder();
                foreach (Vague rv in (sender as GridCheckMarksSelection).Selection)
                {
                    if (sb.ToString().Length > 0) { sb.Append(", "); }
                    sb.Append(rv.Libelle);

                }
                (ActiveControl as GridLookUpEdit).Text = sb.ToString();

            }
        }

        private void OnCustomDrawElement(object sender, DevExpress.XtraGauges.Core.Primitive.CustomDrawElementEventArgs e) {
            e.Handled = true;
            StateIndicatorComponent indicator = sender as StateIndicatorComponent;
            Point p1 = Point.Empty;
            Point p2 = Point.Empty;
            Point p3 = Point.Empty;
            int height = (int)(e.Info.BoundBox.Width * Math.Sqrt(3) / 2);
            p1.X = (int)(e.Info.BoundBox.Width / 2);
            p2.X = (int)e.Info.BoundBox.Left;
            p3.X = (int)e.Info.BoundBox.Right;
            int y = (int)((e.Info.BoundBox.Height - height) / 2);
            Color stateColor = Color.Empty;
            if(indicator.StateIndex == 0) {
                p1.Y = y;
                p2.Y = p3.Y = y +height;
                stateColor = positiveColor;
            }
            else {
                p1.Y = y + height;
                p2.Y = p3.Y = y;
                stateColor = negativeColor;
            }
            e.Context.Graphics.FillPolygon(new SolidBrush(stateColor), new Point[] { p1, p2, p3 });
        }

        private void labelControl1_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Code lors du changement de la vague selectionné, cette vague sera enlevé des support REF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lueVague_EditValueChanged(object sender, EventArgs e)
        {
          

            /*glueVagueRef.Properties.DataSource = _vagues.Where(v => v.id != ((Vague)lueVague.EditValue).id);
            glueVagueRef.Properties.DisplayMember = "Libelle";*/
            
        }

        private void btnCalculer_Click(object sender, EventArgs e)
        {
            var eventProfil = new EventArgsProfil()
            {
                Support = (SupportsView)_supports.First(sup => sup.Nom == lueSupport.EditValue),
                Vague = (Vague)lueVague.EditValue,
                Filter = (Filter)lueFiltre.EditValue,
                SupportRef = listeSupportsViewsRef,
                VagueRef =listeVaguesViewsRef,
                Distribution = cbDistribution.SelectedIndex+1
                
                
            };
            var handlerProfile = ChangeProfilEvent;
            if (handlerProfile != null)
                handlerProfile(this, eventProfil);
        }

        private void labelControl4_Click(object sender, EventArgs e)
        {

        }

        private void lueVagueRef_EditValueChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// La valeurs selectionné dans els supports , enlever cette valeurs parmi les supports REF
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lueSupport_EditValueChanged(object sender, EventArgs e)
        {

           
            
        }

        private void glueSupportsRef_CustomDisplayText(object sender, XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            GridCheckMarksSelection gridCheckMark = (sender as GridLookUpEdit).Properties.Tag as GridCheckMarksSelection;
            if (gridCheckMark == null) return;
            listeSupportsViewsRef=new List<SupportsView>();
            foreach (SupportsView rv in gridCheckMark.Selection)
            {
                if (sb.ToString().Length > 0) { sb.Append(", "); }
                sb.Append(rv.Nom);
                listeSupportsViewsRef.Add(rv);
            }
            e.DisplayText = sb.ToString();
        }



        void gridCheckMarks_SelectionChanged(object sender, EventArgs e)
        {
            if (ActiveControl is GridLookUpEdit)
            {
                int i = 0;
                StringBuilder sb = new StringBuilder();
                foreach (SupportsView rv in (sender as GridCheckMarksSelection).Selection)
                {
                    if (sb.ToString().Length > 0) { sb.Append(", "); }
                    sb.Append(rv.Nom);
                    
                }
                (ActiveControl as GridLookUpEdit).Text = sb.ToString();

            }
        }



    }

    public class EventArgsProfil
        {
        public SupportsView Support { get; set; }
        public List<SupportsView> SupportRef { get; set; }
            public Filter Filter { get; set; }
            public Vague Vague { get; set; }
            public List<Vague> VagueRef { get; set; }
            public int Distribution { get; set; }
            public int Evolution { get; set; }
        }
    
}
